package cn.lf.android.lawfirm.utils.timepicker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.view.wheelview.OnWheelScrollListener;
import cn.lf.android.lawfirm.view.wheelview.WheelView;
import cn.lf.android.lawfirm.view.wheelview.adapter.NumericWheelAdapter;

public class ChooseTime {
    private int mYear = 1996;
    private int mMonth = 0;
    private int mDay = 1;
    private TextView done;
    private WheelView year;
    private WheelView month;
    private WheelView day;
    private WheelView hour;
    private WheelView minute;
    private View view = null;
    private Context mContext;
    private LayoutInflater inflater = null;
    /**
     * 当前选中月份
     **/
    private int nowMonth = 0;
    /**
     * 当前选中年
     **/
    private int nowYear = 0;
    /**
     * 当前选中月份总天数
     **/
    private int nowMonth_allday = 0;
    /**
     * 滑动条数量
     **/
    private int DIMENSIONNUMBER = 3;

    /**
     * 是否校验小时,默认校验
     * DIMENSIONNUMBER=4 时生效
     */
    private boolean checkTime = true;

    public WheelView getYear() {
        return year;
    }

    public WheelView getDay() {
        return day;
    }

    public WheelView getMonth() {
        return month;
    }

    public ChooseTime(Context context, OnClickListener itemsOnClick) {
        this.mContext = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
    }

    public void SetDimensionNumber(int number) {
        DIMENSIONNUMBER = number;
    }

    private List<String> mlist = new ArrayList<String>();// = new ArrayList<String>()

    public void SetList(List<String> number) {
        mlist = number;
    }

    public View getDataPick(OnClickListener itemsOnClick) {
        // Calendar c = Calendar.getInstance();
        int curYear = mYear;
        int curMonth = mMonth + 1;
        int curDate = mDay;
        view = inflater.inflate(R.layout.wheel_date_picker, null);
        done = (TextView) view.findViewById(R.id.done);
        done.setOnClickListener(itemsOnClick);
        year = (WheelView) view.findViewById(R.id.year);
        month = (WheelView) view.findViewById(R.id.month);
        day = (WheelView) view.findViewById(R.id.day);

        if (1 == DIMENSIONNUMBER) {
            DebugLog.e("DIMENSIONNUMBER==" + DIMENSIONNUMBER);
            NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
                    mContext, 0, mlist.size(), mlist, "%02d");
            numericWheelAdapter2.setLabel("");
            year.setViewAdapter(numericWheelAdapter2);
            year.setCyclic(false);// 是否可循环滑动
        } else {
            NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
                    mContext, 1950, 2500);
            numericWheelAdapter1.setLabel("年");
            year.setViewAdapter(numericWheelAdapter1);
            year.setCyclic(true);// 是否可循环滑动
            year.addScrollingListener(scrollListener);
        }

        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
                mContext, 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCurrentItem(curMonth - 1);
        month.addScrollingListener(scrollListener);
        month.setCyclic(true);

        initDay(curYear, curMonth);
        day.setCyclic(true);
        day.addScrollingListener(scrollListener);

        year.setVisibleItems(7);// 设置显示行数
        month.setVisibleItems(7);
        day.setVisibleItems(7);

        year.setCurrentItem(curYear - 1950);

        day.setCurrentItem(curDate - 1);
        done.setTextColor(0xffffffff);

        if (1 == DIMENSIONNUMBER) {
            month.setVisibility(View.GONE);
            day.setVisibility(View.GONE);
        }
        if (4 == DIMENSIONNUMBER) {
            hour = (WheelView) view.findViewById(R.id.time);
            hour.setVisibility(View.VISIBLE);
            hour.setVisibleItems(7);
            initHour();
            hour.setCyclic(true);
            hour.addScrollingListener(scrollListener);
            int curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            if (curHour == 23) {
                curHour = 0;
            } else {
                curHour++;
            }
            hour.setCurrentItem(curHour, false);
            setDoneButStyle();
        }
        if (5 == DIMENSIONNUMBER) {
            hour = (WheelView) view.findViewById(R.id.time);
            hour.setVisibility(View.VISIBLE);
            hour.setVisibleItems(7);
            initHour();
            hour.setCyclic(true);
            hour.addScrollingListener(scrollListener);
            int curHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            if (curHour == 23) {
                curHour = 0;
            } else {
                curHour++;
            }
            hour.setCurrentItem(curHour, false);
            minute = (WheelView) view.findViewById(R.id.min);
            minute.setVisibility(View.VISIBLE);
            minute.setVisibleItems(7);
            initMin();
            minute.setCyclic(true);
            minute.addScrollingListener(scrollListener);
            int curMinute = Calendar.getInstance().get(Calendar.MINUTE);
            if (curMinute == 59) {
                curMinute = 0;
            } else {
                curMinute++;
            }
            minute.setCurrentItem(curMinute, false);
            setDoneButStyle();
        }

        return view;
    }

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = year.getCurrentItem() + 1950;// 年
            int n_month = month.getCurrentItem() + 1;// 月
            nowYear = year.getCurrentItem() + 1950;// 年
            initDay(n_year, n_month);

        }
    };

    private void initDay(int n_year, int n_month) {
        nowMonth_allday = getDay(n_year, n_month);
        nowMonth = n_month;
        nowYear = n_year;
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                mContext, 1, getDay(n_year, n_month), "%02d");
        numericWheelAdapter.setLabel("日");
        day.setViewAdapter(numericWheelAdapter);
        setDoneButStyle();
    }

    /**
     * 设置 按钮样式
     */
    public void setDoneButStyle() {
        if (compareDate()) {
            done.setTextColor(0xffffffff);
        } else if (!compareDate()) {
            done.setTextColor(0xff999999);
        }
    }

    private void initHour() {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                mContext, 0, 23, "%02d");
        numericWheelAdapter.setLabel("时");
        hour.setViewAdapter(numericWheelAdapter);
    }

    private void initMin() {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(
                mContext, 0, 59, "%02d");
        numericWheelAdapter.setLabel("分");
        minute.setViewAdapter(numericWheelAdapter);
    }

    /**
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            case 3:
                day = 31;
                break;
            case 4:
                day = 30;
                break;
            case 5:
                day = 31;
                break;
            case 6:
                day = 30;
                break;
            case 7:
                day = 31;
                break;
            case 8:
                day = 31;
                break;
            case 9:
                day = 30;
                break;
            case 10:
                day = 31;
                break;
            case 11:
                day = 30;
                break;
            case 12:
                day = 31;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    /**
     * 比较日期
     */
    public boolean compareDate() {
        if (!checkTime) {
            return true;
        }
        Boolean boolean1 = true;
        if (year.getCurrentItem() + 1950 < mYear) {
            boolean1 = false;
        }

        if ((year.getCurrentItem() + 1950) == mYear
                && (month.getCurrentItem() + 1) < (mMonth + 1)) {
            boolean1 = false;
        }

        if ((year.getCurrentItem() + 1950) == mYear
                && (month.getCurrentItem() + 1) == (mMonth + 1)
                && (day.getCurrentItem() + 1) < mDay) {
            boolean1 = false;
        }

        if (DIMENSIONNUMBER == 4) {
            if (hour != null) {
                if ((year.getCurrentItem() + 1950) == mYear
                        && (month.getCurrentItem() + 1) == (mMonth + 1)
                        && (day.getCurrentItem() + 1) == mDay) {
                    Calendar date = Calendar.getInstance();
                    if (hour.getCurrentItem() <= date.get(Calendar.HOUR_OF_DAY)) {
                        boolean1 = false;
                    }
                }
            }
        }
        return boolean1;
    }

    public String getTxt() {
        if (1 == DIMENSIONNUMBER) {
            return returnText();
        } else {
            return returnTime();
        }
    }

    private String returnText() {
        return mlist.get(year.getCurrentItem());
    }

    private String returnTime() {
        String string = "";
        String str_year = nowYear + "-";
        // int int_month =getMonth().getCurrentItem() + 1 ;
        String str_day = "";
        if (getDay().getCurrentItem() + 1 > nowMonth_allday) {
            // 得到天数大于本月天数
            if (nowMonth == 2) {
                if (getDay().getCurrentItem() + 1 == 31) {
                    switch (nowYear % 4) {
                        case 0:
                            str_day = "02";
                            break;
                        default:
                            str_day = "03";
                            break;
                    }
                } else if (getDay().getCurrentItem() + 1 == 30) {
                    switch (nowYear % 4) {
                        case 0:
                            str_day = "01";
                            break;
                        default:
                            str_day = "02";
                            break;
                    }
                } else if (getDay().getCurrentItem() + 1 == 29) {
                    str_day = "01";
                }
            } else {
                str_day = "01";
            }
        } else {
            str_day = setDataFormat(getDay().getCurrentItem() + 1);
        }

        String str_month = setDataFormat(nowMonth) + "-";
        switch (nowYear % 4) {
            case 0:// 闰年
                break;
            default:
                if (getDay().getCurrentItem() + 1 == 29 && nowMonth == 2) {
                    DebugLog.e("不是闰年2月的29");
                    str_day = "01";
                }
                break;
        }
        string = str_year + str_month + str_day;
        if (4 == DIMENSIONNUMBER) {
            int hour1 = hour.getCurrentItem();
            string = str_year + str_month + str_day + " " + (hour1 < 10 ? "0" + hour1 : hour1) + ":00";
        }
        if (5 == DIMENSIONNUMBER) {
            int hour1 = hour.getCurrentItem();
            int min = minute.getCurrentItem();
            string = str_year + str_month + str_day + " " + (hour1 < 10 ? "0" + hour1 : hour1) + ":" + (min < 10 ? "0" + min : min);
        }
        return string;
    }

    private String setDataFormat(int i) {
        String string;
        if (i < 10) {
            string = "0" + i;
        } else {
            string = "" + i;
        }
        return string;
    }

    public WheelView getHour() {
        return hour;
    }

    public int getDimensionnumber() {
        return DIMENSIONNUMBER;
    }

    public void setCheckTime(boolean checkTime) {
        this.checkTime = checkTime;
    }
}
